<?php

class Repository extends AbstractRepository implements IRepository
{

  /**
   *
   * @var array
   */
  protected static $instances = array();

  /**
   *
   * @param string $name
   * @return AbstractRepository
   */
  public static function getInstance($name)
  {
    if (empty(self::$instances[$name])) {
      self::$instances[$name] = new self($name);
    }

    return self::$instances[$name];
  }

  /**
   *
   * @param string $name
   * @return void
   */
  protected function __construct($name)
  {
    parent::__construct($name);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IRepository#delete($obj)
   */
  public function delete(IDomObj $obj)
  {
    //
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IRepository#save($obj)
   */
  public function save(IDomObj $obj)
  {
    $data = $obj->getModified();

    $def = Domain::toEntity($this->def);

    if ($obj->isNew()) {
      $data = $this->_insert($def, $data);

      $pk = $def->getPk();
      $obj->$pk = $data[$def->getPkName()];
    }
    else {
      $data[$def->getPkName()] = $obj->get($def->getPk());
      $this->_update($def, $data);
    }
  }

  /**
   *
   * @param array $params
   * @return mixed
   */
  /*public function findCount($params = array())
  {
    $data = parent::findCount($params);

    $mappingDepth = sy_get_param($params, 'mappingDepth', 0);

    if ($mappingDepth == 0) {
      return $data;
    }

    return AbstractFactory::factoryCollection($this->def, $data, $mappingDepth);
  }*/

  /**
   *
   * @param array $params
   * @return mixed
   */
  public function find($params = array())
  {
    $data = parent::_find($params);

    if (empty($data)) {
      return null;
    }

    $mappingDepth = sy_get_param($params, 'mappingDepth', 0);

    if ($mappingDepth == 0) {
      return $data;
    }

    return AbstractFactory::factory($this->def, $data, $mappingDepth);
  }

  /**
   *
   * @param array $params
   * @return array
   */
  public function findAll($params = array())
  {
    $data = parent::_findAll($params);

    $mappingDepth = sy_get_param($params, 'mappingDepth', 0);

    if ($mappingDepth == 0) {
      return $data;
    }

    return AbstractFactory::factoryCollection($this->def, $data, $mappingDepth);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/RepositoryDAO#_findBy($field, $name, $value, $params)
   */
  protected function _findBy($field, $value, $params = array())
  {
    $data = parent::_findBy($field, $value, $params);

    $mappingDepth = sy_get_param($params, 'mappingDepth', 0);

    if ($mappingDepth == 0) {
      return $data;
    }

    return AbstractFactory::factory($this->def, $data, $mappingDepth);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/RepositoryDAO#_findAllBy($field, $value, $params)
   */
  protected function _findAllBy($field, $value, $params = array())
  {
    $data = parent::_findAllBy($field, $value, $params);

    $mappingDepth = sy_get_param($params, 'mappingDepth', 0);

    if ($mappingDepth == 0) {
      return $data;
    }

    return AbstractFactory::factoryCollection($this->def, $data, $mappingDepth);
  }

}

?>